package com.project_doctor.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project_doctor.pojo.Institution;
import com.project_doctor.pojo.common.MyPageData;
import com.project_doctor.pojo.dto.InstitutionSearchDTO;
import com.project_doctor.pojo.dto.ResidentTagDTO;
import com.project_doctor.service.InstitutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/institution")
public class InstitutionController {
    @Autowired
    private InstitutionService institutionService;

    @PostMapping("/getInstitutionInfo/{pageNum}/{pageSize}")
    public ResponseEntity<?> getInstitutionInfo(@PathVariable Integer pageNum,
                                                @PathVariable Integer pageSize,
                                                @RequestBody(required = false) InstitutionSearchDTO searchDTO) {
        Page<Institution> page = institutionService.getInstitutionInfo(pageNum, pageSize, searchDTO);
        List<Institution> institutionList = page.getRecords();
        MyPageData myPageData = new MyPageData(page.getTotal(), institutionList);
        return ResponseEntity.ok(myPageData);
    }

    @PostMapping("/addInstitution")
    public ResponseEntity<?> addInstitution(@RequestBody Institution institution) {
        institutionService.save(institution);
        return ResponseEntity.ok("添加完成");
    }

    @PutMapping("/updateInstitution")
    public ResponseEntity<?> updateInstitution(@RequestBody Institution institution) {
        institutionService.updateInstitution(institution);
        return ResponseEntity.ok("更新完成");
    }

    @DeleteMapping("/deleteInstitution/{iId}")
    public ResponseEntity<?> deleteInstitution(@PathVariable Long iId) {
        institutionService.removeById(iId);
        return ResponseEntity.ok("删除完成");
    }

    @GetMapping("/getInstitution") // 获取签约机构
    public ResponseEntity<?> getInstitution(){
        List<Institution> institutions = institutionService.list();
        return ResponseEntity.ok(institutions);
    }
}
